import { Toast } from 'antd-mobile'
import * as _ from 'lodash'
import { createAction, NavigationActions } from '../../utils/index'
import * as Libs from '../../utils/Libs'

import * as forecastService from '../../services/forecast/forecastService'

const mockD = {
  tax: '10',  // 支出金额（含税）
  pay: '5', //支出金额（不含税）
  dpay: '', //可抵扣增值税额
  qpay: '5', //其他税额
  funds: '5', //资金来源
  fundsHand: '10', //自有资金
  loan: '3', //借款
  allYear: '3', //项目总年限
  startYear: '2', //预计从第几年开始正式取得收益
  oneIncome:'5', //预测第一年收入（含税）
  oneNoIncome: '2', //预测第一年收入（不含税）
  limits: '3', //税额
  incomeGrowth: '1', //收入增长率
  yycb: '3', //营业成本收入比
  xsfy: '3', //销售费用收入比
  glfy: '3', //管理费用收入比
  cwfy: '3', //财务费用收入比
  zjsr: '3', //折旧收入比
  ycmqs: '3', //预测末期处置全部权益可回收或需支付的资金
  srsy: '12', //收入适用的增值税税率,
  yycbzz:'3', //营业成本适用的增值税税率
  zcje: '10', //支出金额适用的增值税税率
  fjsl: '3', //附加税率
  zzsfh: '3', //增值税返还率
  sds: '3', //所得税率
  jkll: '3', //借款利率
  tzl: '3', //通胀率
}

const initTzyc = {
  tax: '',  // 支出金额（含税）
  pay: '', //支出金额（不含税）
  dpay: '', //可抵扣增值税额
  qpay: '', //其他税额
  funds: '', //资金来源
  fundsHand: '', //自有资金
  loan: '', //借款
  allYear: '', //项目总年限
  startYear: '', //预计从第几年开始正式取得收益
  oneIncome:'', //预测第一年收入（含税）
  oneNoIncome: '', //预测第一年收入（不含税）
  limits: '', //税额
  incomeGrowth: '', //收入增长率
  yycb: '', //营业成本收入比
  xsfy: '', //销售费用收入比
  glfy: '', //管理费用收入比
  cwfy: '', //财务费用收入比
  zjsr: '', //折旧收入比
  ycmqs: '', //预测末期处置全部权益可回收或需支付的资金
  srsy: '', //收入适用的增值税税率,
  yycbzz:'', //营业成本适用的增值税税率
  zcje: '', //支出金额适用的增值税税率
  fjsl: '', //附加税率
  zzsfh: '', //增值税返还率
  sds: '', //所得税率
  jkll: '', //借款利率
  tzl: '', //通胀率
}

export default {
  namespace: 'tzyc',
  state: initTzyc,
  reducers: {
    initState(state, { payload}) {
      return {...initTzyc}
    },
    changeState(state, {payload}) {
      return {...state, ...payload}
    },
    resetSecondDetail(state, {payload}) {
      return {...state, secondRespD: {}}
    }
  },
  effects: {
    *tzycOne({ payload }, { call, select, put}) {
      const tzycState = yield select(state => state.tzyc)
      const params = {}
      params.tax = tzycState.tax
      params.zcje = tzycState.zcje
      params.pay = tzycState.pay
      params.fundsHand = tzycState.fundsHand
      params.oneIncome = tzycState.oneIncome
      params.srsy = tzycState.srsy
      const resp = yield call(forecastService.getTzycFirst, params)
      if (resp.resultCode !== 200) {
        Toast.info(resp.msg, 1)
        return false
      }
      const respD = resp.data || {}
      let newRespD = {}
      _.map(respD, (v, k) => {
        newRespD[k] = v + ""
      })
      yield put(createAction("changeState")(newRespD))
    },
    //详细结果
    *tzycTwo({ payload}, { call, select, put}) {
      const tzycState = yield select(state => state.tzyc)
      const fullParams = Object.assign({}, payload, tzycState)

      const resp = yield call(forecastService.getTzycTwo, fullParams)
      if (resp.resultCode !== 200) {
        Toast.info(resp.msg, 1)
        return false
      }
      const respD = resp.data || {}
      console.log('respD is' , respD)

      const topData = forecastService.geneTopData(respD.result)

      const firstTitles  = forecastService.secondGetTitles(respD.trzj)
      const secondTitles = forecastService.secondGetTitles(respD.qzbj)

      const totalYear = ((respD.years || {}).year || {}).total || []

      const firstArr = forecastService.secondGetRight(totalYear, respD.trzj || {})
      const secondArr = forecastService.secondGetRight(totalYear, respD.qzbj || {})

      const params = {
        secondRespD: {
          topData: topData,
          firstTitles: firstTitles,
          firstArr: firstArr,
          secondTitles: secondTitles,
          secondArr: secondArr
        }
      }
      console.log('navigate to tzyc result with ', params)
      yield put(NavigationActions.navigate({
        routeName: 'TzYcResultStack',
        params:  params
      }))
    }
  },
  subscriptions: {
  },
}



let mockResp = {
  "resultCode": 200,
  "msg": "success",
  "data": {
    "qzbj": {
      "xjlr": {
        "total": [
          0,
          0,
          0,
          2500,
          2874.99,
          3306.24
        ],
        "details": {
          "titles": [
            "产品销售收入",
            "销项税额"
          ],
          "contents": [
            [
              0,
              0
            ],
            [
              0,
              0
            ],
            [
              0,
              0
            ],
            [
              2136.75,
              363.25
            ],
            [
              2457.26,
              417.73
            ],
            [
              2825.85,
              480.39
            ]
          ]
        }
      },
      "xmjzjthbz": {
        "total": [
          -1500,
          0,
          0,
          672.67,
          612.16,
          611.65
        ],
        "details": null
      },
      "xjzc": {
        "total": [
          1500,
          0,
          0,
          1764.95,
          2186,
          2597.17
        ],
        "details": {
          "titles": [
            "期初支出金额",
            "总成本费用",
            "所得税费用",
            "进项税额及实际已纳增值税"
          ],
          "contents": [
            [
              1500,
              0,
              0,
              0
            ],
            [
              0,
              0,
              0,
              0
            ],
            [
              0,
              0,
              0,
              0
            ],
            [
              0,
              1559.83,
              23.5,
              181.62
            ],
            [
              0,
              1808.07,
              26.32,
              351.61
            ],
            [
              0,
              2086.9,
              29.88,
              480.39
            ]
          ]
        }
      },
      "ljjxjllthbz": {
        "total": [
          -1500,
          -1500,
          -1500,
          -827.33,
          -215.17,
          396.48
        ],
        "details": null
      },
      "xmjll": {
        "total": [
          -1500,
          0,
          0,
          735.05,
          688.99,
          709.07
        ],
        "details": null
      },
      "ljjxjll": {
        "total": [
          -1500,
          -1500,
          -1500,
          -764.95,
          -75.96,
          633.11
        ],
        "details": null
      }
    },
    "trzj": {
      "xjlr": {
        "total": [
          0,
          0,
          0,
          2500,
          2874.99,
          3306.24
        ],
        "details": {
          "titles": [
            "产品销售收入",
            "销项税额"
          ],
          "contents": [
            [
              0,
              0
            ],
            [
              0,
              0
            ],
            [
              0,
              0
            ],
            [
              2136.75,
              363.25
            ],
            [
              2457.26,
              417.73
            ],
            [
              2825.85,
              480.39
            ]
          ]
        }
      },
      "xmjzjthbz": {
        "total": [
          -1500,
          -225.3,
          -212.03,
          387.31,
          331.44,
          332.94
        ],
        "details": null
      },
      "xjzc": {
        "total": [
          1500,
          232.06,
          224.94,
          2076.78,
          2501.95,
          2920.27
        ],
        "details": {
          "titles": [
            "期初支出金额",
            "总成本费用",
            "归还本金",
            "所得税费用",
            "进项税额及实际已纳增值税"
          ],
          "contents": [
            [
              1500,
              0,
              0,
              0,
              0
            ],
            [
              0,
              42.75,
              200,
              -10.69,
              0
            ],
            [
              0,
              33.25,
              200,
              -8.31,
              0
            ],
            [
              0,
              1583.58,
              200,
              111.58,
              181.62
            ],
            [
              0,
              1822.32,
              200,
              128.02,
              351.61
            ],
            [
              0,
              2091.65,
              200,
              148.23,
              480.39
            ]
          ]
        }
      },
      "ljjxjllthbz": {
        "total": [
          -1500,
          -1725.3,
          -1937.33,
          -1550.02,
          -1218.58,
          -885.64
        ],
        "details": null
      },
      "xmjll": {
        "total": [
          -1500,
          -232.06,
          -224.94,
          423.22,
          373.04,
          385.97
        ],
        "details": null
      },
      "ljjxjll": {
        "total": [
          -1500,
          -1732.06,
          -1957,
          -1533.78,
          -1160.74,
          -774.77
        ],
        "details": null
      }
    },
    "years": {
      "year": {
        "total": [
          2017,
          2018,
          2019,
          2020,
          2021,
          2022
        ],
        "xq": null
      }
    }
  }
}
